<?php declare(strict_types=1);

/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Order\Ctl;

use Srv\Apps\Proto\VO\Order\PayGoogleKeyListVO;
use Srv\Apps\Proto\VO\Order\PayGoogleKeyVO;
use Srv\Conf\ConfModel\ConfExt;
use Srv\Conf\ConfModel\GPlayAuthConf;
use Srv\Libs\Common\CommArray;
use Srv\Libs\Common\CommJson;
use Srv\Libs\Frame\ControllerAbstract;
use Srv\Libs\Plugins\GooglePlay\GooglePlay;
use Srv\Libs\Storage\DataMysql;
use Srv\Libs\Storage\DataRedis;

final class PayGoogleCtl extends ControllerAbstract
{
    private ?DataMysql $DbAuto              = null;                 // DB-Write
    private const GOOGLE_AUTH_GROUP_NAME    = 'play';               // 配置分组名
    private ?GPlayAuthConf $GoogleAuthConf  = null;                 // PayAppleConf
    private ?DataRedis $CcAuto              = null;                 // Cache
    private int $CacheExpire                = 600;                  // Cache时长[秒]
    private string $CacheKey                = 'PAY_GOOGLE_KEY_LIST'; // Cache存储Key

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        $thisObject                         = self::tagGet();
        if($thisObject instanceof self){
            $thisObject->DbAuto             = self::getDbAuto();
            $thisObject->CcAuto             = self::getCacheAuto();
            $thisObject->GoogleAuthConf     = ConfExt::getGPlayAuthConfGroup()->getByGroupName(self::GOOGLE_AUTH_GROUP_NAME);
        }
        return $thisObject;
    }

    /**
     * @return PayGoogleKeyListVO|null
     */
    public function getPayGoogleKeyList():?PayGoogleKeyListVO{

        $PayGoogleKeyListVO     = new PayGoogleKeyListVO();
        $list                   = $this->CcAuto->getAry($this->CacheKey);
        if(count($list) < 1 || !isset($list[0]['sku'])){
            $list               = (new GooglePlay($this->GoogleAuthConf))->getInAppProductList($this->GoogleAuthConf->getAndroidBundleId());
            if(count($list) > 0){
                $this->CcAuto->set($this->CacheKey, CommJson::encodeArray($list), $this->CacheExpire);
            }
        }
        if(CommArray::isForeach($list)){
            $PayGoogleKeyVOList  = [];
            foreach ($list as $item){
                if(!is_array($item) || count($item) < 1 || !isset($item['sku'])) continue;
                $title                  = $item['listings']['en-US']['title']??'';
                $sku                    = $item['sku'];
                $status                 = strtoupper($item['status']??'');
                $label                  = $this->str_pad_user($title, 32, ' ').' | '.$this->str_pad_user($sku, 32, ' ').' | '.$status;
                $PayGoogleKeyVO         = new PayGoogleKeyVO();
                $PayGoogleKeyVO->setTitle($title);
                $PayGoogleKeyVO->setSku($sku);
                $PayGoogleKeyVO->setStatus($status);
                $PayGoogleKeyVO->setLabel($label);
                $PayGoogleKeyVOList[]   = $PayGoogleKeyVO;
            }
            $PayGoogleKeyListVO->setList($PayGoogleKeyVOList);
        }
        return $PayGoogleKeyListVO;
    }

    /**
     * @param string $str
     * @param int $length
     * @param string $padString
     * @return string
     * str_pad_user
     */
    private function str_pad_user(string $str, int $length=32, string $padString=''):string
    {
        $displayCount    = (strlen($str) - mb_strlen($str))/2;
        return str_pad($str, $length + $displayCount, $padString);
    }
}